<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The bootstrap base class can be used to manupilate the request, pass
 * variables to views and layouts, add routes to the router, validate and
 * create an ACL e.t.c.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Core.Application
 */
abstract class Dmf_Application_Bootstrap
{
    /**
     * The application class
     * 
     * @var Dmf_Application 
     */
    private $_application;
    
    /**
     * The autoloader
     *
     * @var Dmf_Autoloader
     */
    private $_autoloader;
    
    /**
     * The configuration library
     * 
     * @var Dmf_Config_Ini|Dmf_Config_Php
     */
    private $_config;
    
    /**
     * The router
     * 
     * @var Dmf_Router
     */
    private $_router;
    
    /**
     * Constructor
     * 
     * @param Dmf_Application $app
     */
    public function __construct( Dmf_Application $app )
    {
        $this->_application = $app;
        $this->_autoloader = $app->getAutoloader();
        $this->_config = $app->getConfig();
    }
    
    /**
     * This event will be fired when all needed-core files
     * and settings are loaded. When this method isn't called
     * an error is created with the highest priority. 
     */
    public function __initApplication()
    {
        
    }
    
    /**
     * This event will be fired just before the router is dispatched
     * you can manupilate the router request or add a custom route to
     * the router.
     * 
     * @param Dmf_Router $router
     * @param Dmf_Request $request 
     */
    public function __routerPredispatch( Dmf_Router &$router, Dmf_Request &$request )
    {
        
    }
    
    /**
     * This event will be fired after the router is dispatched and the
     * controller is loaded.
     */
    public function __routerDispatch()
    {
        
    }
    
    /**
     * Run the entire application, you can't override this method because
     * you don't need to. 
     */
    public final function __runApplication()
    {
        //Clean all content first
        @ob_clean();
        
        //Call application initate first
        $this->__initApplication();
        
        ob_start();
        
        $requestString = Dmf_Request::cleanRequest( $_SERVER['REQUEST_URI'] );
        
        $this->_router = new Dmf_Router( $this );
        
        if ( $this->_config->asObject()->router->load_rules && file_exists( 'application/configs/default.rules' ) )
        {
            $this->_router->loadRulesFile( 'application/configs/default.rules' );
        }
        
        if ( $this->_config->asObject()->router->execute_rules )
        {
            foreach( $this->_router->getRewriteRules() as $rule )
            {
                $requestString = preg_replace( '/' . $rule['0'] . '/', $rule['1'], $requestString );
            }
        }
        
        $this->_request = new Dmf_Request( $requestString );
        
        $this->__routerPredispatch( $this->_router, $this->_request );
        
        $this->_router->dispatch( $this->_request );
        
        $this->__routerDispatch();
    }
    
    /**
     * This event will be fired when the full page is preRendered, after the
     * dispatcher and before controllers are loaded and the page is rendered.
     * In this event you can pass variables to the layout or view for example.
     * 
     * @param Dmf_Layout $layout
     * @param Dmf_View $view 
     */
    public function __templateRendered( Dmf_Layout &$layout, Dmf_View &$view )
    {
        
    }
}